
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for add_to_favorites.
	''' </summary>
	Public Partial Class add_to_favorites
		Inherits BaseWebPage
		Protected lblMessageUserNotExist As System.Web.UI.WebControls.Label
		Protected grpID As Integer = -1
		Protected id As Integer = -1
		Protected isGroupMode As Boolean = False
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.ADD_TO_FAVORITES_PAGE;
			Dim usr As User = Me.LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.ADD_TO_FAVORITES, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			btnCancel.Attributes("onclick") = "javascript: history.back(); return false;"

			' hide Message labels
			lblAddToFavoritesMessage.Visible = False
			lblAddYourselfToFavourites.Visible = False

			' hide user details control
			trUserShortCut.Visible = False

			If Request.Params("id") <> Nothing AndAlso Request.Params("id") <> "" Then
				' add user to favorites
				Try
					id = Convert.ToInt32(Request.Params("id"))
					Dim userToAdd As User = Users.GetUserByID(id)
					If userToAdd.ID = LoggedUser.ID Then
						lblAddToFavoritesMessage.Visible = False
						btnAddToFavorites.Visible = False
						btnCancel.Visible = False
						lblAddYourselfToFavourites.Visible = True
					Else
						lblAddYourselfToFavourites.Visible = False
						If userToAdd = Nothing Then
							lblAddToFavoritesMessage.Visible = True
							lblAddToFavoritesMessage.Text = GetPageConstText(htPageConstants, "CONST_MsgUserNotExist_TEXT")
							btnAddToFavorites.Visible = False
							btnCancel.Visible = False
						Else
							lblAddToFavoritesMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgQAddUser_TEXT")).Replace("[PARAM]", userToAdd.Name)
							lblAddToFavoritesMessage.Visible = True
							ucMemberShortcut.UserID = id
							trUserShortCut.Visible = True
						End If
					End If
				Catch
					lblAddToFavoritesMessage.Text = GetPageConstText(htPageConstants, "CONST_MsgUnexpectedError_TEXT")
					

					lblAddToFavoritesMessage.Visible = True
					btnAddToFavorites.Visible = False
					btnCancel.Visible = False
				End Try
ElseIf Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
				' add group to favorites	
				Try
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
					Dim grpToAdd As Group = Groups.GetGroupByID(grpID)
					If grpToAdd = Nothing Then
						Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
					End If

					isGroupMode = True
					lblAddYourselfToFavourites.Visible = False
					lblAddToFavoritesMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgQAddGroup_TEXT")).Replace("[PARAM]", grpToAdd.Name)
					lblAddToFavoritesMessage.Visible = True
					hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
					hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grpToAdd.Name)
				Catch
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End Try
			Else
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		Protected Sub btnAddToFavorites_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim usr As User = Me.LoggedUser
			Try
				If isGroupMode Then
					Dim grp As Group = Groups.GetGroupByID(grpID)
					If usr.FavoriteGroups.Contains(grpID) Then
						lblAddToFavoritesMessage.Text = GetPageConstText(htPageConstants, "CONST_MsgGroupAdded_TEXT")
						lblAddToFavoritesMessage.Visible = True
						btnAddToFavorites.Visible = False
						btnCancel.Visible = False
					Else
						usr.FavoriteGroups.Add(grp)
						Response.Redirect(Constants.VIEW_FAVORITE_GROUPS)
					End If
				Else
					Dim id As Integer = Convert.ToInt32(Request.Params("id"))
					Dim userToAdd As User = Users.GetUserByID(id)
					If userToAdd <> Nothing Then
						If usr.FavoriteUsers.Contains(userToAdd.ID) Then
							lblAddToFavoritesMessage.Text = GetPageConstText(htPageConstants, "CONST_MsgUserAdded_TEXT")
							lblAddToFavoritesMessage.Visible = True
							btnAddToFavorites.Visible = False
							btnCancel.Visible = False
						Else
							usr.FavoriteUsers.Add(New BO.FavoriteUser(userToAdd.ID))
							Response.Redirect(Constants.VIEW_FAVORITE_USERS)
						End If
					End If
				End If
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try
		End Sub

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
        Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_Form1
            End Get
        End Property
        'set { Migrated_Form1 = value; }
    End Class
End Namespace






